﻿using System;
using System.Text;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using webapp4U;
using webapp4U.BOL;

namespace webapp4U.UI
{
    public partial class Marquee : Page
    {
        int BosuutapID;
   
        protected void Page_Load(object sender, EventArgs e)
        {
            BosuutapID = Globals.GetIntFromQueryString("BosuutapID");
            Load_BosuutapName(BosuutapID);

            if (Globals.GetStringFromQueryString("Action") == "Delete")
            {
                try
                {
                    int SlideShowID = Globals.GetIntFromQueryString("SlideShowID");
                    SlideShow.Delete(SlideShowID);
                    Response.Redirect("SlideShow.aspx?BosuutapID=" + BosuutapID);
                }
                catch
                {
                    Response.Redirect("SlideShow.aspx?BosuutapID=" + BosuutapID);
                }
            }

            this.Load_SlideShow();
        }


        private void Load_BosuutapName(int BosuutapID)
        {
            Bosuutap obj = Bosuutap.GetBosuutapByID(BosuutapID);
            lblBosuutapName.Text = obj.Title;
        }


        private void Load_SlideShow()
        {
            List<SlideShow> list = SlideShow.GetSlideShowByBosuutapID(BosuutapID,1,1000);
            ItemsDataList.DataSource = list;
            ItemsDataList.DataBind();
        }


        protected string FilePhoto_Upload()
        {
            string strFileName = "";
            // kiem tra xem user co chon hinh dai dien ko
            if (PhotoFile.HasFile)
            {
                // kiem tra phan mo rong cua file co hop le hay khong
                string strExtension = System.IO.Path.GetExtension(PhotoFile.FileName).ToUpper();
                if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".BMP" || strExtension == ".PNG")
                {
                    // lay ten file de khong bi trung file + phan mo rong cua file can upload
                    strFileName = Common.GetFileName() + System.IO.Path.GetExtension(PhotoFile.FileName);

                    //duong dan tao save file Large
                    string strFilePathLarge = Request.PhysicalApplicationPath + "//UserUpload//Bosuutap//" + strFileName;

                    //thuc hien save file Large len server
                    EnumUpload uploadLarge = FileProcess.UpLoadImage(PhotoFile, strFilePathLarge, -1);

                    //Neu upload file Large thanh cong
                    if (uploadLarge == EnumUpload.UploadSucceed)
                    {
                        return strFileName;
                    }
                    else
                    {
                        // loi xay ra khi upload hinh
                        if (uploadLarge == webapp4U.EnumUpload.FileLarge)
                        {
                            // size toi da cho phep upload len server
                            int intPhotoSizeMax = Convert.ToInt32(ConfigurationManager.AppSettings["PhotoSizeMax"]);
                            lblMsg.CssClass = "error";
                            lblMsg.Text = "Bạn phải chọn hình có kích thước nhỏ hơn " + intPhotoSizeMax.ToString() + " KB";
                            strFileName = "";
                        }
                    }
                }
                else
                {
                    lblMsg.CssClass = "error";
                    lblMsg.Text = "File bạn chọn upload không hợp lệ.";
                    strFileName = "";
                }
            }

            return strFileName;
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            try
            {
                string ImageUrl = FilePhoto_Upload();
                if (ImageUrl.Length > 3)
                {
                    SlideShow obj = new SlideShow();
                    obj.AddedDate = DateTime.Now;
                    obj.BosuutapID = BosuutapID;
                    obj.IsActive = true;
                    obj.ImageUrl = ImageUrl;
                    obj.Title = txtTitle.Text;

                    SlideShow.Insert(obj);

                    Response.Redirect("SlideShow.aspx?BosuutapID=" + BosuutapID);

                    lblMsg.CssClass = "lblMsg";
                    lblMsg.Text = "Upload hình thành công!";
                    Load_SlideShow();
                }
            }
            catch
            { Response.Redirect("SlideShow.aspx?BosuutapID=" + BosuutapID); }
        }
}
}